﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

namespace ZhimeiSoft.Framework.Client.Context
{
    public class ClientContext
    {
        private static ClientContext context;

        private Hashtable additions = new Hashtable();

        private ClientContext()
        {
        }

        public static ClientContext Properties
        {
            get 
            {
                if (ClientContext.context == null)
                    ClientContext.context = new ClientContext();
                return ClientContext.context;
            }
        }

        public static IDictionary Contexts
        {
            get 
            {
                IDictionary result;
                if (ClientContext.context != null)
                    result = ClientContext.context.additions;
                else
                    result = null;
                return result;
            }
        }

        public string this[string key]
        {
            get
            {
                object syncRoot;
                Monitor.Enter(syncRoot = this.additions.SyncRoot);
                string result;
                try
                {
                    result = (string)this.additions[key];
                }
                finally
                {
                    Monitor.Exit(syncRoot);
                }
                return result;
            }
            set
            {
                object syncRoot;
                Monitor.Enter(syncRoot = this.additions.SyncRoot);
                try
                {
                    if (this.additions.ContainsKey(key))
                    {
                        string strA = (string)this.additions[key];
                        if (string.Compare(strA, key, true) != 0)
                        {
                            this.additions[key] = value;
                        }
                    }
                    else
                    {
                        this.additions.Add(key, value);
                    }
                }
                finally
                {
                    Monitor.Exit(syncRoot);
                }
            }
        }

        public static void Remove(string key)
        {
            if (context != null && context.additions.ContainsKey(key))
            {
                context.additions.Remove(key);
            }
        }

        public static void Remove(string[] keys)
        {
            if (context != null && keys != null)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    Remove(keys[i]);
                }
            }
        }

        public static void DestoryInstance()
        {
            if (context != null)
                context = null;
        }

        public override string ToString()
        {
            string result;
            if (this.additions == null || this.additions.Count <= 0)
                result = string.Empty;
            else
            {
                StringBuilder sbLog = new StringBuilder(1024);
                object syncRoot;
                Monitor.Enter(syncRoot = this.additions.SyncRoot);
                try
                {
                    IDictionaryEnumerator enumerator = this.additions.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        string arg = enumerator.Key.ToString();
                        string arg2 = enumerator.Value.ToString();
                        sbLog.AppendFormat("{0}={1}", arg, arg2);
                        sbLog.AppendLine();
                    }
                }
                finally
                {
                    Monitor.Exit(syncRoot);
                }
                result = sbLog.ToString();
            }
            return result;
        }

    }
}
